clear all;
clc;

tic
disp('Loading data...');
load mnist_test;
load mnist_train;
clear xL;
clear xU;

k = 7;
s = 100;
lam = 1;
n = 70000;

C = 10;

Y = yL';
Y = [(Y==0) (Y==1) (Y==2) (Y==3) (Y==4) (Y==5) (Y==6) (Y==7) (Y==8) (Y==9)];
Y = Y - (Y==0);
Y = double(Y);
[l,c] = size(Y);
disp('Data loaded!');

load 7nneigsplus;

V = V(:,1:s);
n = size(V,1);
clear Ivec;
clear IDXvec;

P = zeros(l,s,c);
for i = 1:c
    P(:,:,i) = repmat(Y(:,i),1,s).*V(1:l,:);
end
clear Y;
Q = V(l+1:n,:);
clear V;

disp('Start optimization...');
tic

cvx_begin sdp
cvx_solver sedumi
    variable t
    variable gam(s,c)
    variable M(s,s) symmetric
    variable del(l,1) nonnegative
    minimize (t-lam*trace(M))
    subject to
        M > 0
        [M gam(:,1);gam(:,1)' t-2*C*ones(1,l)*del] >= 0
        P(:,:,1)*gam(:,1) >= ones(l,1)-del
        [M gam(:,2);gam(:,2)' t-2*C*ones(1,l)*del] >= 0
        P(:,:,2)*gam(:,2) >= ones(l,1)-del
        [M gam(:,3);gam(:,3)' t-2*C*ones(1,l)*del] >= 0
        P(:,:,3)*gam(:,3) >= ones(l,1)-del
        [M gam(:,4);gam(:,4)' t-2*C*ones(1,l)*del] >= 0
        P(:,:,4)*gam(:,4) >= ones(l,1)-del
        [M gam(:,5);gam(:,5)' t-2*C*ones(1,l)*del] >= 0
        P(:,:,5)*gam(:,5) >= ones(l,1)-del
        [M gam(:,6);gam(:,6)' t-2*C*ones(1,l)*del] >= 0
        P(:,:,6)*gam(:,6) >= ones(l,1)-del
        [M gam(:,7);gam(:,7)' t-2*C*ones(1,l)*del] >= 0
        P(:,:,7)*gam(:,7) >= ones(l,1)-del
        [M gam(:,8);gam(:,8)' t-2*C*ones(1,l)*del] >= 0
        P(:,:,8)*gam(:,8) >= ones(l,1)-del
        [M gam(:,9);gam(:,9)' t-2*C*ones(1,l)*del] >= 0
        P(:,:,9)*gam(:,9) >= ones(l,1)-del
        [M gam(:,10);gam(:,10)' t-2*C*ones(1,l)*del] >= 0
        P(:,:,10)*gam(:,10) >= ones(l,1)-del
cvx_end

toc

[~,Ypred] = max(Q*gam,[],2);
Ypred = uint8(Ypred)-1;

label = int8(yU');
calacc(Ypred,label)